package com.bootdo.xzl.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.StringUtils;
import com.bootdo.common.utils.UploadUtils;
import com.bootdo.xzl.dao.HousesImgInfoDao;
import com.bootdo.xzl.dao.SecondhandGoodsDao;
import com.bootdo.xzl.domain.HousesImgInfoDO;
import com.bootdo.xzl.domain.SecondhandGoodsDO;
import com.bootdo.xzl.service.SecondhandGoodsService;

@Service
public class SecondhandGoodsServiceImpl implements SecondhandGoodsService {

	@Autowired
	private SecondhandGoodsDao secondhandGoodsDao;
	
	@Autowired
	private HousesImgInfoDao housesImgInfoDao;
	
	@Value("${file.path}")
	private String filePath;

	
	@Override
	public SecondhandGoodsDO get(Integer id) {
		return secondhandGoodsDao.get(id);
	}

	@Override
	public List<SecondhandGoodsDO> list(Map<String, Object> map) {
		return secondhandGoodsDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map) {
		return secondhandGoodsDao.count(map);
	}

	@Override
	public int save(SecondhandGoodsDO secondhandGoods) {
		
		secondhandGoodsDao.save(secondhandGoods);
		
		if (null != secondhandGoods && null != secondhandGoods.getFiles() && secondhandGoods.getFiles().size() > 0) {
			// 获取关联文件
			List<MultipartFile> files = secondhandGoods.getFiles();
			int max = files.size();
			List<HousesImgInfoDO> images = new ArrayList<>(max);
			HousesImgInfoDO housesImgInfoDO = null;

			for (int i = 0; i < max; i++) {
				housesImgInfoDO = new HousesImgInfoDO();
				// 房源id
				housesImgInfoDO.setOfficeId(secondhandGoods.getId());
				// 图片编号
				housesImgInfoDO.setImgNo(generatePrimarykey());
				// 上传文件名
				housesImgInfoDO.setFileName(files.get(i).getOriginalFilename());
				// 磁盘物理文件名
				String physicalFileName = afterUploadFileName(files.get(i).getOriginalFilename());
				housesImgInfoDO.setPhysicalFileName(physicalFileName);
				// 上传人
				if (null != secondhandGoods.getCreateBy()) {
					housesImgInfoDO.setCreateBy(String.valueOf(secondhandGoods.getCreateBy()));
				}
				// 文件类型
				String filetype = getSuffixName(files.get(i).getOriginalFilename());
				housesImgInfoDO.setFileType(filetype);
				// 文件大小
				housesImgInfoDO.setFileSize(files.get(i).getSize());
				//业务类型
				housesImgInfoDO.setImgFlg("1");
				// 上传文件
				try {
					UploadUtils.writeFile(files.get(i), filePath, physicalFileName);
				} catch (IOException e) {
					e.printStackTrace();
					return 0;
				}
				// 添加集合
				images.add(housesImgInfoDO);
			}
			housesImgInfoDao.save(images);
		}
		return 1;
	}

	@Override
	public int update(SecondhandGoodsDO secondhandGoods) {
		
		// 删除磁盘文件
		if(null!=secondhandGoods && secondhandGoods.getDelImagesIds()!=null && secondhandGoods.getDelImagesIds().size()>0){
				// 查询文件列表
			   Integer[] array =  secondhandGoods.getDelImagesIds().toArray(new  Integer[secondhandGoods.getDelImagesIds().size()]);
			   List<HousesImgInfoDO> images = housesImgInfoDao.getImgListByIds(array);
			   if(null!=images && images.size()>0 ){
				    int max = images.size();
				    // 删除磁盘文件
				    File file  = null;
				   for(int i=0;i<max;i++){
					   file = new File(filePath+images.get(i).getPhysicalFileName());
					   if(file.exists()){
						   file.delete();
					   }
				   }
			   }
			 //删除数据库文件
			 housesImgInfoDao.batchUpdate(images);
		}
		
		// 上传文件
		if(null!=secondhandGoods && null!=secondhandGoods.getFiles() && secondhandGoods.getFiles().size()>0){
				 // 获取关联文件
				 List<MultipartFile> files =  secondhandGoods.getFiles();
				 int max = files.size();
				 List<HousesImgInfoDO> images =  new ArrayList<>(max);
				 HousesImgInfoDO housesImgInfoDO =  null;
			
			 for(int i=0;i<max;i++){
						 housesImgInfoDO = new HousesImgInfoDO();
						 // 房源id
						 housesImgInfoDO.setOfficeId(secondhandGoods.getId());
						 // 房源编号	
						 housesImgInfoDO.setOfficeHousesNumber(generatePrimarykey());
						 // 图片编号
						 housesImgInfoDO.setImgNo(generatePrimarykey());
						 // 上传文件名
						 housesImgInfoDO.setFileName(files.get(i).getOriginalFilename());
						 // 磁盘物理文件名
						 String physicalFileName = afterUploadFileName(files.get(i).getOriginalFilename());
						 housesImgInfoDO.setPhysicalFileName(physicalFileName);
						 // 上传人
						 if(null!=secondhandGoods.getCreateBy()){
							 housesImgInfoDO.setCreateBy(String.valueOf(secondhandGoods.getCreateBy()));
						 }
						 // 文件类型
						 String filetype = getSuffixName(files.get(i).getOriginalFilename());
						 housesImgInfoDO.setFileType(filetype);
						 //文件大小
						 housesImgInfoDO.setFileSize(files.get(i).getSize());
						 // 上传文件
							//业务类型
							housesImgInfoDO.setImgFlg("1");
						try {
							UploadUtils.writeFile(files.get(i), filePath, physicalFileName );
						} catch (IOException e) {
							e.printStackTrace();
							return 0;
						}
						// 添加集合
						images.add(housesImgInfoDO);
			 }
			 housesImgInfoDao.save(images);
		}
		return secondhandGoodsDao.update(secondhandGoods);
	}

	@Override
	public int batchUpdate(Integer[] ids) {

		return secondhandGoodsDao.batchUpdate(ids);
	}

	@Override
	public int remove(Integer id) {
		return secondhandGoodsDao.remove(id);
	}

	@Override
	public int batchRemove(Integer[] ids) {
		return secondhandGoodsDao.batchRemove(ids);
	}

	@Override
	public SecondhandGoodsDO secondhandGoodsInfo(Integer id) {
		
		SecondhandGoodsDO secondhandGoodsDO = secondhandGoodsDao.secondhandGoodsInfo(id);
		
		if(null!=secondhandGoodsDO){
				// 查询文件列表
			   Integer[] array =  new Integer[1];
			   array[0] = secondhandGoodsDO.getId();
			   List<HousesImgInfoDO> images = housesImgInfoDao.getImgListByofficeId(array,"1");
			   //  添加图片
			   if(images!=null && images.size()>0){
				   secondhandGoodsDO.setHousesImgInfoList(images);
			   }
		}
		return secondhandGoodsDO;
	}
	
	

	// 获取上传后的文件名
	public String afterUploadFileName(String type) {
		if (StringUtils.isEmpty(type)) {
			return "";
		}
		long r = (long) ((Math.random() * 9 + 1) * 100000);
		long s = Calendar.getInstance().getTimeInMillis();
		String t = String.valueOf(r) + String.valueOf(s) + UploadUtils.getSuffixName(type);
		return t;
	}

	// 房源编号
	public String generatePrimarykey() {
		long r = (long) ((Math.random() * 9 + 1) * 10000000);
		long s = Calendar.getInstance().getTimeInMillis();
		String key = String.valueOf(r) + String.valueOf(s);
		return key;
	}

	// 获取文件后缀名
	public String getSuffixName(String fileName) {
		if (StringUtils.isEmpty(fileName)) {
			return "";
		} else if (fileName.indexOf(fileName) < 0) {
			return "";
		}
		String suffix = fileName.substring(fileName.lastIndexOf("."));
		return suffix;
	}

}
